package turrets;
import Enemies.*;
import Generator.Land;
import Manage.Counter;
import Manage.maze;
import info.gridworld.actor.Actor;
import info.gridworld.grid.Location;
import java.awt.Color;
import java.util.ArrayList;
public class Turret extends Actor{
	private static int price =480;
	protected int damage;
	protected int attackspeed;
	protected int sellprice;
	protected int upgradeprice;
	protected int range;
	protected int numupgrades;
	protected int cooldown;
	private Enemy target;
	private ArrayList<Location> path;
	public Turret(){
		Counter.addMoney(-price);
		path = maze.getPath();
		target = null;
		range = 2;
		sellprice = price /2;
		upgradeprice = price/4;
		attackspeed = 1;
		damage = 15;
		numupgrades = 0;
		cooldown = attackspeed;
	}
	public int getPrice(){
		return price;
	}
	public void Sell(){
		Counter.addMoney(sellprice);
		Location loc = getLocation();
		removeSelfFromGrid(loc);
		Land d = new Land();
		d.putSelfInGrid(getGrid(), loc);
		d.setColor(Color.GREEN);
	}
	protected void setValues(int da, int sp, int ra){
		damage = da;
		attackspeed = sp;
		range = ra;
	}
	public int getSellPrice(){
		return sellprice;
	}
	public int getDamage(){
		return damage;
	}
	public String upgrade(){
		if(numupgrades == 3)
			return "Turret is already fully upgraded.";
		if (Counter.getMoney() - upgradeprice < 0){
			return "There is not enough money for that transaction.";
		}else{
			Counter.addMoney(-upgradeprice);
			sellprice = sellprice+(upgradeprice/2);
			upgradeprice = upgradeprice + (price/4);
			switch (numupgrades){
			case 0:
				damage = (int) (damage*1.15);
				break;
			case 1:
				damage = (int) (damage * 1.3);
				break;
			case 2:
				damage = (int) (damage * 1.45);
				break;
			default:
				damage = damage *90000;	
			}
			numupgrades++;
			return "Turret Upgraded";
		}
	}
	public ArrayList<Enemy> getEnemies(){
		ArrayList<Enemy> enemies = new ArrayList<Enemy>();
		int srow, scol;
		srow = getLocation().getRow() - range;
		scol = getLocation().getCol() - range;
		for(int r =srow;r<=srow +(range*2); r++){
			for(int c = scol;c<=scol+(range*2);c++){
				for(int i = getGrid().getNumDep()-1; i>-1;i--){
				Location loc = new Location(r,c,i);
				if(getGrid().isValid(loc)&&getGrid().get(loc) instanceof Enemy)
					enemies.add((Enemy) getGrid().get(loc));
				}
			}
		}
		return enemies;
	}
	protected void act(){
		if(cooldown == -1){
			cooldown = attackspeed;
		}
		if(cooldown == attackspeed){
			ArrayList<Enemy>enemies = getEnemies();
			Enemy enemy = aim(enemies);
			attack(enemy);
		}
		cooldown--;
	}
	public Enemy aim(ArrayList<Enemy> enemies){
		if(target != null){
			for(int x = 0;x<enemies.size();x++){
				if(enemies.get(x).equals(target))
					return enemies.get(x);
		}	
		}
		for(int x = path.size()-1; x>=0;x--){
			for(int y = 0;y<enemies.size();y++){
				if(enemies.get(y).getLocation().equals(path.get(x))){
					target = enemies.get(y);
					return enemies.get(y);
				}
			}
		}
		return null;
	}
	public String getUpGradeInfo(){
		String s = "Upgrade Number       % increase        Total damage     Price\n" + 
				   "Base                                0%                     15                          480\n" + 
				   "1st                                  15%                     17                         120\n" +
				   "2nd                                  30%                    22                         240\n" +
				   "3rd                                  45%                     31                        360";
		return s; 
	}
	public void playSound(){
		new AePlayWave("TurretShot.wav").start();
	}
	public void attack(Enemy enemy){
		if(enemy!=null){
			setDirection(getLocation().getDirectionToward(enemy.getLocation()));
			enemy.takeDamage(damage);
			playSound();
		}
		else
			cooldown++;
		}
}
